<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="servlets.*"%>
<%@ page import="org.hibernate.SessionFactory"%>
<%@ page import="org.hibernate.Session"%>
<%@ page import="java.util.List"%>
<%@ page import="hibernate.model.*"%>
<%@ page import="hibernate.util.HibernateUtil"%>
<%@ page import="hibernate.method.*"%>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<script>
$("a.locals").click(function() {
		$("#mainPage_content").hide().fadeIn(1000).load("main/localProfile.jsp?local_id="+ $(this).attr('id'));
		return false;
	});
</script>

<div class="page-header">
	<h2 class="page-title">Nasze lokale</h2>
</div>
<div id="content">
<br />
<%
						SessionFactory sessionFactory = HibernateUtil
								.getSessionAnnotationFactory();
						Session s = sessionFactory.openSession();
						s.beginTransaction();
						LocalMethods lm = new LocalMethods();
							response.setContentType("text/html");
							List<Local> locals = lm.listAllLocals(sessionFactory, s); 
							FeedbackMethods fb = new FeedbackMethods();
					%> 
			
<%
		if (locals.size() > 0) { %>
		<table data-height="299" data-card-view="true">
		<thead>
<tr>
<th data-field="name"> Lokal: </th>
	<th data-field="Rate"> Ogólna ocena:</th>
<th data-field="comments">Liczba komentarzy</th>
<th data-field="description"> Opis: </th>
</tr>
</thead>
<tbody>
<%
	for (Local l : locals) {
		int local_id = l.getLocal_id();
		String localName = l.getCity() + ", "+ l.getStreet()+ " "+l.getLocal_number();
		%>
		
										<tr>
										<td>
													<%out.println("<a class=\"locals\" id=\""+ local_id + "\" href=\"#\">" + localName + "</a>"); %></h4>
										</td>
										</tr>
										<tr>
										<td>
													<%RateMethods rm = new RateMethods();
													out.println("Ogólna ocena uzytkownikow:"+ rm.returnAverageRateByLocal(sessionFactory, s, local_id)); %>
											</td>
											</tr>
											<td>
											<% 	int commentsCount = fb.countFbByLocalId(sessionFactory, s, local_id);
													out.println("Komentarzy: "+ commentsCount); %>
											</td><td>
													<% String opis1 = null;
													if (l.getDescription()== null || l.getDescription().equals("0")) opis1 = "Brak";
													else opis1 = l.getDescription();%>
													<%out.println("Opis lokalu: "+ opis1);%>
											</td>		
											</tr>
		
								
		<%
		}
	%>
	</tbody>
	</table>
	<%
		}
		s.getTransaction().commit();
		%>

</div>		
		